// pages/login/login.js
import { $wuxCountDown } from '../../dist-wux/index'
import { $wuxNotification } from '../../dist-wux/index'

var app = getApp()
var httpUtils = require('../../utils/httpUtils')
var api = require("../../utils/API")
var utils = require("../../utils/utils")
var commonFields = require("../../utils/commonFields")
const isTel = (value) => !/^1[34578]\d{9}$/.test(value)
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mobile: "",
        validateCode: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    showNotification() {
        this.closeNotification = $wuxNotification().show({
            // image: 'http://pbqg2m54r.bkt.clouddn.com/logo.png',
            title: '提示',
            text: '手机号错误!请正确输入!',
            data: {
                message: '手机号错误!请正确输入!!'
            },
            duration: 3000,
            onClick(data) {
                console.log(data)
            },
            onClose(data) {
                console.log(data)
            },
        })
    },

    onInputChange(e) {
        console.log("login input onChang", e)
        let id = e.currentTarget.id
        let value = utils.trim(e.detail.value)
        console.log("input id", id)
        console.log("input value", value)

        if (id == "mobile") {
            this.setData({
                mobile: value,
            })
        } else if (id == "validateCode") {
            this.setData({
                validateCode: value,
            })
        }
    },
    vcode(e) {
        if (this.c2 && this.c2.interval) return !1
        console.log(e)
        let mobile = this.data.mobile
        if (isTel(mobile)) {
            this.showNotification()
            return;
        }
        httpUtils.postRequest(api.API_USER_VALI_CODE,
            {
                mobile: mobile,
                purpose: "1",
            },
            res => {
                console.log(res)
            },
            fail => {
                console.log(fail)
            }
        );
        this.c2 = new $wuxCountDown({
            date: +(new Date) + 90000,
            onEnd() {
                this.setData({
                    c2: '重新获取验证码',
                })
            },
            render(date) {
                const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
                date.sec !== 0 && this.setData({
                    c2: sec,
                })
            },
        })
    },

    /**
     * 手机登录
     */
    loginByMobile(e) {
        console.log(e)
        let mobile = e.currentTarget.dataset.mobile
        let validateCode = this.data.validateCode
        console.log("mobile", mobile)
        console.log("validateCode", validateCode)
        let that = this;
        httpUtils.postRequest(api.API_USER_LOGIN,
            {
                openid: utils.getStorageSync(commonFields.openid),
                wxName: utils.getStorageSync(commonFields.wxUserInfo).nickName,
                wxHeadImg: utils.getStorageSync(commonFields.wxUserInfo).avatarUrl,
                mobile: mobile,
                shareCode: utils.getStorageSync(commonFields.shareCode),
                validateCode: validateCode,
                purpose: "1",
            },
            res => {
                utils.setStorageSync(commonFields.userInfo, res.data)
                wx.showToast({
                    title: '登录成功',
                    icon: 'success',
                    duration: 2000
                })
                wx.reLaunch({
                    url: api.PAGES_MOCKEXAMS_MOCKEXAMS
                })
            },
            fail => {
                console.log(fail)
            }
        );
    },
})